package com.bong.ring.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.bong.ring.R;


/**
 * TODO: document your custom view class.
 */
public class CicleView extends View {
    private int color, colorBack;
    private float colorWidth, percent;

    public CicleView(Context context) {
        super(context);
        init(null, 0);
    }

    public CicleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public CicleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.CicleView, defStyle, 0);
        color = a.getColor(R.styleable.CicleView_cicleColor, Color.WHITE);
        colorBack = a.getColor(R.styleable.CicleView_cicleBackColor, Color.BLACK);
        colorWidth = a.getDimension(R.styleable.CicleView_cicleWidth, 3);
        percent = a.getFloat(R.styleable.CicleView_ciclePercent, 0.5f);
        a.recycle();

        // Update TextPaint and text measurements from attributes
        invalidateTextPaintAndMeasurements();
    }

    private void invalidateTextPaintAndMeasurements() {

        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.STROKE);
    }

    Paint paint = new Paint();

    @TargetApi(21)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float percent=tempPercent;
        // TODO: consider storing these as member variables to reduce
        // allocations per draw cycle.
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();

        int contentWidth = getWidth() - paddingLeft - paddingRight;
        int contentHeight = getHeight() - paddingTop - paddingBottom;
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(colorWidth);
        paint.setColor(colorBack);
        if (contentHeight > contentWidth) {
            canvas.drawCircle(paddingLeft + contentHeight / 2, paddingTop + contentHeight / 2, contentWidth / 2 - paint.getStrokeWidth() / 2, paint);
            paint.setColor(color);
            canvas.drawArc(getPaddingLeft() + paint.getStrokeWidth() / 2,
                    getPaddingTop() + paint.getStrokeWidth() / 2,
                    getPaddingLeft() + contentWidth - paint.getStrokeWidth() / 2,
                    getPaddingTop() + contentHeight - paint.getStrokeWidth() / 2,
                    0, percent * 360, false, paint);
        } else {
            canvas.drawCircle(paddingLeft + contentHeight / 2, paddingTop + contentHeight / 2, contentHeight / 2 - paint.getStrokeWidth() / 2, paint);
            paint.setColor(color);
            canvas.drawArc(getPaddingLeft() + paint.getStrokeWidth() / 2,
                    getPaddingTop() + paint.getStrokeWidth() / 2,
                    getPaddingLeft() + contentWidth - paint.getStrokeWidth() / 2,
                    getPaddingTop() + contentHeight - paint.getStrokeWidth() / 2,
                    -90, percent * 360, false, paint);
        }
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(0);
        canvas.drawCircle(paddingLeft + contentHeight / 2, getPaddingTop() + colorWidth / 2, colorWidth / 2, paint);
        canvas.save();
        canvas.rotate(percent * 360, paddingLeft + contentHeight / 2, paddingTop + contentHeight / 2);
        canvas.drawCircle(paddingLeft + contentHeight / 2, getPaddingTop() + colorWidth / 2, colorWidth / 2, paint);
        canvas.restore();
        if(tempPercent!=this.percent)
        {
            if(tempPercent>this.percent)
            {
                tempPercent-=0.02f;
                if(tempPercent<this.percent)
                    tempPercent=this.percent;
                invalidate();
            }
            if(tempPercent<this.percent)
            {
                tempPercent+=0.02f;
                if(tempPercent>this.percent)
                    tempPercent=this.percent;
                invalidate();
            }
        }
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, @Nullable Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    }

    private float tempPercent = 0;

    private void beginAnim() {

    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public int getColorBack() {
        return colorBack;
    }

    public void setColorBack(int colorBack) {
        this.colorBack = colorBack;
    }

    public float getColorWidth() {
        return colorWidth;
    }

    public void setColorWidth(float colorWidth) {
        this.colorWidth = colorWidth;
    }

    public float getPercent() {
        return percent;
    }

    public void setPercent(float percent) {
        this.percent = percent;
    }
}
